package com.inspinia.base.api.controller;

import com.inspinia.base.api.model.InterfaceUserDataAuthority;
import com.inspinia.base.api.service.InterfaceUserDataAuthorityService;
import com.inspinia.base.common.model.AjaxResult;
import com.inspinia.base.pagination.Pager;
import com.inspinia.base.util.CollectionUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
* 的Controller
* Created by pujw on 2017-6-30.
*/
@Controller
@RequestMapping("interfaceUserDataAuthority")
public class InterfaceUserDataAuthorityController{

    @Resource
    private InterfaceUserDataAuthorityService interfaceUserDataAuthorityService;

    /**
    * 跳转分页页面
    *
    * @return
    */
    @RequestMapping(value = "paging", method = RequestMethod.GET)
    public String toPaginationPage() {
        return "interfaceUserDataAuthority/interfaceUserDataAuthority-pagination";
    }

    /**
    * 分页查询
    *
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "paging", method = RequestMethod.POST)
    public AjaxResult paging(InterfaceUserDataAuthority interfaceUserDataAuthority) {
        Pager<InterfaceUserDataAuthority> page = interfaceUserDataAuthorityService.findPage(interfaceUserDataAuthority);
        return new AjaxResult(true, page);
    }

    /**
    * 创建
    *
    * @param interfaceUserDataAuthority
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public AjaxResult create(@Valid InterfaceUserDataAuthority interfaceUserDataAuthority) {
        interfaceUserDataAuthorityService.insert(interfaceUserDataAuthority);
        return new AjaxResult(true);
    }

    /**
    * 根据id获取
    *
    * @param id id
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public AjaxResult get(long id) {
        InterfaceUserDataAuthority bean = interfaceUserDataAuthorityService.get(id);
        return new AjaxResult(true, bean);
    }

    /**
    * 编辑
    *
    * @param interfaceUserDataAuthority
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "goEdit", method = RequestMethod.POST)
        public AjaxResult edit(@Valid InterfaceUserDataAuthority interfaceUserDataAuthority) {
        interfaceUserDataAuthorityService.update(interfaceUserDataAuthority);
        return new AjaxResult(true);
    }

    /**
    * 删除
    *
    * @param id
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public AjaxResult delete(Long id) {
        int deleted = interfaceUserDataAuthorityService.delete(id);
        if( deleted > 0 ){
        	return new AjaxResult(true);
        }
        return new AjaxResult(false,"删除失败，请重试！");
    }
    
    /**
    * 批量删除
    *
    * @param ids
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "batchDelete", method = RequestMethod.POST)
    public AjaxResult delete(@RequestParam(name = "ids[]", required = false, defaultValue = "") Long[] ids) {
        int deleted = interfaceUserDataAuthorityService.batchDelete(ids);
        if( deleted > 0 && ids.length == deleted ){
        	return new AjaxResult(true);
        }
        return new AjaxResult(false,"有"+(ids.length - deleted)+"个资源删除失败，请重试！");
    }

    /**
     * 修改用户省份,城市权限
     * @param userId
     * @param provinceIds
     * @param cityIds
     * @return
     */
    @RequestMapping(value = "updateUserLocationAuthority",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateUserLocationAuthority(Long userId, List<Long> provinceIds, List<Long> cityIds){
        boolean cityRes = true;
        boolean provinceRes = true;

        interfaceUserDataAuthorityService.deleteByUserId(userId);

        if (userId == null && (CollectionUtil.isNotEmpty(provinceIds) || CollectionUtil.isNotEmpty(cityIds))){
            return new AjaxResult(false,"传入数据不能为空");
        }

        packageUserLocationData(provinceIds,provinceRes,userId,0);
        packageUserLocationData(cityIds,cityRes,userId,1);

        if (cityRes && provinceRes){
            return new AjaxResult(true);
        }

        return new AjaxResult(false,"修改失败!");
    }

    /**
     * 包装用户数据权限数据
     * @param ids
     * @param res
     * @param userId
     * @param j 0为省数据,1为市数据
     */
    private void packageUserLocationData(List<Long> ids, boolean res ,Long userId,int j ){
        if (CollectionUtil.isNotEmpty(ids)){
            for (int i = 0; i < ids.size(); i++) {
                InterfaceUserDataAuthority interfaceUserDataAuthority = new InterfaceUserDataAuthority();
                if (j == 0){
                    interfaceUserDataAuthority.setProvinceId(ids.get(i));
                }else if (j == 1){
                    interfaceUserDataAuthority.setCityId(ids.get(i));
                }
                interfaceUserDataAuthority.setInterfaceUserId(userId);
                res = (interfaceUserDataAuthorityService.update(interfaceUserDataAuthority) == 1 ? true : false) && res;
            }
        }
    }
}